package com.tiyende.kash.loop.ui.presenter

import com.tiyende.kash.loop.common.CommonPresenter
import com.tiyende.kash.loop.configs.TiyendeApiPath
import com.tiyende.kash.loop.configs.TiyendeApiUrl
import com.tiyende.kash.loop.configs.TiyendeApiUrl.addCommonParams
import com.tiyende.kash.loop.request.IHttpCallback
import com.tiyende.kash.loop.ui.contract.IViewContract
import com.tiyende.kash.loop.utils.helper.TiyendeScoreDialogHelper
import org.json.JSONObject

/**
 * TiyendeCredit
 * @Author wwdeng
 * @CreateTime: 2024/9/27 15:38
 * @Annotation：订单详情
 */
class TiyendeOrderDetailsPresenter(private val iView: IViewContract.ITiyendeOrderInfoView): CommonPresenter(iView) {

    /**
     * 获取贷款信息
     */
    fun loadOrderDetailsAction(){
        val httpUrl = TiyendeApiUrl.getTiyendeHttpUrl()
        val params = JSONObject().addCommonParams().apply {
            put("path",TiyendeApiPath.TIYENDE_PATH.TIYENDE_API_LOAN_APPLY_PATH)
            put("showOrderStatus","YES")
            put("orderId",iView.getTiyendeOrderInfoId())
        }
        doHttpPostAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                val obj = data.optJSONObject("obj")
                if (obj != null) {
                    //显示共用View
                    iView.showTiyendeOrderInfoCommonViews(obj)
                    val orderStatus = obj.optString("status")
                    when(orderStatus) {
                        "IN_REVIEW" -> {//审核中
                            iView.showTiyendeOrderInfoReviewingViews()
                            loadOrderDetailsWalletListAction()
                        }
                        "LENDING" -> {//放款中
                            iView.showTiyendeOrderInfoDisbursingViews()
                            loadOrderDetailsWalletListAction()
                        }
                        "REJECT" -> {//已拒绝
                            iView.showTiyendeOrderInfoRejectedViews(obj)
                            //加载是否显示清除用户数据按钮
                            loadUserClearDataAction(true)
                        }
                        else -> { //待还款
                            iView.showTiyendeOrderInfoRepayViews()
                        }
                    }
                }
            }

            override fun onRequestError(error: JSONObject) {
            }

            override fun onRequestFinish() {
                iView.hiddenTiyendeLoading()
            }
        })
    }

    /**
     * 获取账户列表
     */
    fun loadOrderDetailsWalletListAction(){
        val httpUrl = TiyendeApiUrl.getTiyendeHttpUrl()
        val params = JSONObject().addCommonParams().apply {
            put("path", TiyendeApiPath.TIYENDE_PATH.TIYENDE_API_ACCOUNT_LIST_PATH)
        }
        doHttpPostAction(httpUrl, params, object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                val array = data.optJSONArray("obj")
                val accountList = arrayListOf<JSONObject>()

                if (array != null && array.length() > 0) {
                    for (i in 0..<array.length()) {
                        val item = array.optJSONObject(i)
                        item.put("isEdit", false)
                        accountList.add(item)
                    }
                }
                iView.showTiyendeOrderInfoWalletList(accountList)
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
            }
        })
    }

    /**
     * 只有审核中或放款中需要调此接口
     * 获取评分配置
     */
    fun loadScoreCommentAction() {
        val httpUrl = TiyendeApiUrl.getTiyendeHttpUrl()
        val params = JSONObject().addCommonParams().apply {
            put("path", TiyendeApiPath.TIYENDE_PATH.TIYENDE_API_STAR_COMMENT_PATH)
        }
        doHttpPostAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                val obj = data.optJSONObject("obj")
                if (obj != null) {
                    TiyendeScoreDialogHelper.showStarDialog(iView.getAct(),obj,true)
                }
            }

            override fun onRequestError(error: JSONObject) {
            }

            override fun onRequestFinish() {
            }
        })
    }

    /**
     * 清除用户数据 或 获取是否显示删除数据按钮
     * 只有已拒绝才会调用该接口
     */
    fun loadUserClearDataAction(isShowButton: Boolean){
        val httpUrl = TiyendeApiUrl.getTiyendeHttpUrl()
        val params = JSONObject().addCommonParams().apply {
            put("path",TiyendeApiPath.TIYENDE_PATH.TIYENDE_API_USER_DATA_CLEAR_NEW_PATH)
        }
        if (isShowButton){
            params.put("type","GET")
        } else {
            params.put("type","DROP")
        }
        doHttpPostAction(httpUrl, params, object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                iView.hiddenTiyendeLoading()
                if (isShowButton){
                    val obj = data.optJSONObject("obj")
                    if (obj != null && obj.has("showDropButton") && obj.optString("showDropButton") == "YES"){
                        iView.showTiyendeUserDataClearButton(true)
                    } else {
                        iView.showTiyendeUserDataClearButton(false)
                    }
                } else {
                    iView.showTiyendeUserDataClearSuccess()
                }
            }
            override fun onRequestError(error: JSONObject) {
                iView.showTiyendeToast(error.optString("errorMsg"))
            }
            override fun onRequestFinish() {
                iView.hiddenTiyendeLoading()
            }
        })
    }
}